import java.net.*;
import java.io.*;
import java.io.File;
import java.util.*;

public class IssueServer{
    public static void main(String[] args){
        try{
            int serverPort = 7896;
            ServerSocket listenSocket = new ServerSocket(serverPort);
            while(true){
                Socket clientSocket = listenSocket.accept();
                Connection c = new Connection(clientSocket);
            }
        }catch(IOException e){
            System.out.println("Listen " + e.getMessage());
        }
    }
}

class Connection extends Thread{
    DataInputStream in;
    DataOutputStream out;
    Socket clientSocket;
    public Connection(Socket aClientSocket){
        try{
            clientSocket = aClientSocket;
            in = new DataInputStream(clientSocket.getInputStream());
            out = new DataOutputStream(clientSocket.getOutputStream());
            this.start();
        }catch(IOException e){
            System.out.println("Connection " + e.getMessage());
        }
    }
    public void run(){
        try{
            String data = in.readUTF();
            int requestid= 0;
            String res_message="";
            String Msg="";
            String res="";
            FileControl fc = new FileControl("dbfile.txt","count.txt");
            StringTokenizer tokens = new StringTokenizer( data, "\n" );

            for( int x = 1; tokens.hasMoreElements(); x++ )
            {
                String tmp =  tokens.nextToken();
                StringTokenizer tokens2 = new StringTokenizer(tmp , "=" );
                tokens2.hasMoreElements(); 
                String var1=  tokens2.nextToken();
                tokens2.hasMoreElements(); 
                String var2=  tokens2.nextToken();
                System.out.println(var1);
                System.out.println(var2);
                if(var1.equals("requestid")){
                    requestid = Integer.parseInt(var2);
                }

                if(var1.equals("get_list")){
                    res_message = fc.getList(var2);
                }else if(var1.equals("get_string")){
                    res_message = fc.getString(var2);
                }else if(var1.equals("put_issu")){
                    StringTokenizer tokens3 = new StringTokenizer( var2, ":" );
                    String id =  tokens3.nextToken();
                    String status =  tokens3.nextToken();
                    String issue =  tokens3.nextToken();
                    fc.addLine(id+","+status+","+issue);
                    res_message = "메세지 추가";
                }else if(var1.equals("ch_issue")){
                    StringTokenizer tokens3 = new StringTokenizer( var2, ":" );
                    String idx =  tokens3.nextToken();
                    String str =  tokens3.nextToken();
                    if(fc.statuschange(idx,str)){
                        res_message="성공";
                    }else{
                        res_message="실패";
                    }
                }else if(var1.equals("del_issue")){
                    StringTokenizer tokens3 = new StringTokenizer( var2, ":" );
                    String idx =  tokens3.nextToken();
                    if(fc.removeLine(var2)){
                        res_message="성공";
                    }else{
                        res_message="실패";
                    }
                }
            }
            Msg = "messagetype=rep\nrequestid="+requestid + "\nres=\n" + res_message;
            //out.writeUTF(data);
            out.writeUTF(Msg);
        }catch(EOFException e){
            System.out.println("EOF: " + e.getMessage());
        }catch(IOException e){
            System.out.println("IO: " + e.getMessage());
        }finally{
            try{
                clientSocket.close();
            }catch(IOException e){}
        }
    }
}
